package com.qfedu.web;


import com.qfedu.constants.PageEnum;
import com.qfedu.model.MilkTea;
import com.qfedu.page.PageBean;
import com.qfedu.service.MilkTeaMeauService;
import com.qfedu.service.impl.MilkTeaMeauServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.rmi.server.ServerCloneException;
import java.util.List;
import java.util.Map;

@WebServlet("/milkTeaMeau")
public class MilkTeaMeauServlet extends BaseServlet{
    MilkTeaMeauService mtms=new MilkTeaMeauServiceImpl();
    //分页
    public void milkTeaPage(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String pageNo = req.getParameter("pageNo");
        int currentPage=1;
        PageBean<MilkTea> pageBean=new PageBean<>();
        if (pageNo == null){
             pageBean= mtms.findPage(currentPage, PageEnum.DEFAULTSIZE.getSize());
        }else {
            int row = Integer.parseInt(pageNo);
             pageBean = mtms.findPage(row, PageEnum.DEFAULTSIZE.getSize());
        }
        //获取访问路径
        String requestURI = req.getRequestURI();
        pageBean.setUrl(requestURI+"?action=pageBean");
        req.setAttribute("pageBean",pageBean);
        req.getRequestDispatcher("/pages/meau/meau.jsp").forward(req,resp);
    }
    //添加
    public void add(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        String tname = req.getParameter("tname");
        String price = req.getParameter("price");
        BigDecimal price1 = BigDecimal.valueOf(Integer.parseInt(price));
        String genre = req.getParameter("genre");
        String img_path = req.getParameter("img_path");
        resp.setCharacterEncoding("text/html;charset=utf-8");
        MilkTea milkTea=new MilkTea();
        milkTea.setTname(tname);
        milkTea.setPrice(price1);
        milkTea.setGenre(genre);
        String requestURI = req.getRequestURI();
        milkTea.setImg_path(requestURI+"?action=findAll");
        MilkTeaMeauService mms=new MilkTeaMeauServiceImpl();
        int add = mms.add(milkTea);
        if (add>0){
            System.out.println("添加成功");
            req.setAttribute("milkTea",milkTea);
            req.getRequestDispatcher("/pages/meau/meau.jsp").forward(req,resp);
        }
    }
    //删除
    public void delete(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        String tid = req.getParameter("tid");
        int i = Integer.parseInt(tid);
        if (i==0){
            req.setAttribute("message","该商品不存在");
            req.getRequestDispatcher("/error.jsp").forward(req,resp);
        }else {
             mtms.delete(i);
            req.getRequestDispatcher("/pages/meau/meau.jsp").forward(req,resp);
        }
    }
    //修改
    public void update(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Map parameterMap = req.getParameterMap();
        MilkTea milkTea=new MilkTea();
        BeanUtils.populate(milkTea,parameterMap);
        int row = mtms.update(milkTea);
        if (row>0){
            req.getRequestDispatcher("/pages/meau/meau.jsp").forward(req,resp);
        }else {
            req.setAttribute("message","删除失败");
            req.getRequestDispatcher("/error.jsp").forward(req,resp);
        }
    }
    //查询所有
    public void findAll(HttpServletRequest req,HttpServletResponse resp){
        List<MilkTea> list = mtms.findAll();
        req.setAttribute("milkTea",list);
        System.out.println(list);
        try {
            req.getRequestDispatcher("/pages/meau/meau.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
